package com.powernode.commons;

import lombok.Data;
import lombok.ToString;

import java.sql.SQLException;

/**
 * @author X
 * @date 2021/10/28
 * 结果集
 */
@Data
@ToString
public class Result {

    private Integer code;

    private String message;

    private Object data;

    /**
     * 成功时返回的结果
     * @return 结果result中没有数据
     */
    public static Result OK(){
        return new Result(200,"success");
    }

    /**
     * 成功时的返回
     * @param data 有数据,也许是查询到的数据
     * @return result结果中有数据
     */
    public static Result OK(Object data){
        return new Result(200,"success",data);
    }

    /**
     * 报异常时的返回
     * @param e 异常
     * @return 返回的结果
     */
    public static Result build(ResultException e){
        return new Result(e.getCode(),e.getMessage());
    }
    public static Result build(int code, String message){
        return new Result(code,message);
    }

    public Result() {
    }

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
